package com.yuanyu.server.impl;

import com.yuanyu.common.util.AssertUtil;
import com.yuanyu.common.util.CopyUtil;
import com.yuanyu.mapper.mysqlMapper.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_1_Mapper;
import com.yuanyu.mapper.mysqlMapper.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_2_Mapper;
import com.yuanyu.mapper.mysqlMapper.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_3_Mapper;
import com.yuanyu.mapper.mysqlMapper.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_Mapper;
import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.model.dto.Initial_maintenance_inspection_reportDTO;
import com.yuanyu.model.entity.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report;
import com.yuanyu.model.entity.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_1;
import com.yuanyu.model.entity.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_2;
import com.yuanyu.model.entity.Initial_maintenance_inspection_report.Initial_maintenance_inspection_report_3;
import com.yuanyu.model.vo.Initial_maintenance_inspection_reportVO;
import com.yuanyu.model.dto.MainTableDTO;
import com.yuanyu.server.ITestPart1Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class TestPart1ServerImpl implements ITestPart1Server {
    @Autowired
    private Initial_maintenance_inspection_report_Mapper initial_maintenance_inspection_report_mapper;
    @Autowired
    private Initial_maintenance_inspection_report_1_Mapper initial_maintenance_inspection_report_1_mapper;
    @Autowired
    private Initial_maintenance_inspection_report_2_Mapper initial_maintenance_inspection_report_2_mapper;
    @Autowired
    private Initial_maintenance_inspection_report_3_Mapper initial_maintenance_inspection_report_3_mapper;
    @Autowired
    private MainTable_Mapper mainTableMapper;

    @Override
    public String test() {
        Initial_maintenance_inspection_report_1 test1 = initial_maintenance_inspection_report_1_mapper.selTable1("znic10086");
        Initial_maintenance_inspection_report_2 test2 = initial_maintenance_inspection_report_2_mapper.selTable2("znic10086");
        Initial_maintenance_inspection_report_3 test3 = initial_maintenance_inspection_report_3_mapper.selTable3("znic10086");
        System.out.println(test1);
        System.out.println(test2);
        System.out.println(test3);
        return "测试成功";
    }

    @Override
    public String test1(Initial_maintenance_inspection_reportVO initialMaintenanceInspectionReportVO) {
        //先判断Case是否存在
        Initial_maintenance_inspection_report test = initial_maintenance_inspection_report_mapper.selTableTime(initialMaintenanceInspectionReportVO.getInput_Case());
        AssertUtil.isNull(test,"Case已存在,如需修改内容请前往后台修改");
        //时间表
        Initial_maintenance_inspection_report initialMaintenanceInspectionReport = new Initial_maintenance_inspection_report();
        //子表1
        Initial_maintenance_inspection_report_1 initialMaintenanceInspectionReport1 = new Initial_maintenance_inspection_report_1();
        //子表2
        Initial_maintenance_inspection_report_2 initialMaintenanceInspectionReport2 = new Initial_maintenance_inspection_report_2();
        //子表3
        Initial_maintenance_inspection_report_3 initialMaintenanceInspectionReport3 = new Initial_maintenance_inspection_report_3();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport);
        //时间表 获取当前时间
        initialMaintenanceInspectionReport.setCreatedAt(LocalDateTime.now());
        initialMaintenanceInspectionReport.setUpdatedAt(LocalDateTime.now());
        //子表1 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport1);
        //子表2 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport2);
        //子表3 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport3);

        System.out.println(initialMaintenanceInspectionReport1);
        System.out.println(initialMaintenanceInspectionReport2);
        System.out.println(initialMaintenanceInspectionReport3);

        //判断是否出现异常,出现异常返回"添加数据出现异常,请检查",并且回滚数据
        int i = initial_maintenance_inspection_report_mapper.addTableTime(initialMaintenanceInspectionReport);
        int i1 = initial_maintenance_inspection_report_1_mapper.addTable1(initialMaintenanceInspectionReport1);
        int i2 = initial_maintenance_inspection_report_2_mapper.addTable2(initialMaintenanceInspectionReport2);
        int i3 = initial_maintenance_inspection_report_3_mapper.addTable3(initialMaintenanceInspectionReport3);
        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1 && i3 == 1,"添加数据出现异常,请检查");

        mainTableMapper.updateByCaseFormStartTime(initialMaintenanceInspectionReportVO.getInput_Case());
        return "添加成功";

    }

    @Override
    public Initial_maintenance_inspection_reportDTO getTest1(String Case) {
        //获取每个子表的信息,并判断,如果不存在就返回"Case不存在"
        Initial_maintenance_inspection_report initialMaintenanceInspectionReport = initial_maintenance_inspection_report_mapper.selTableTime(Case);
        if (initialMaintenanceInspectionReport == null)return null;
        Initial_maintenance_inspection_report_1 initialMaintenanceInspectionReport1 = initial_maintenance_inspection_report_1_mapper.selTable1(Case);
        Initial_maintenance_inspection_report_2 initialMaintenanceInspectionReport2 = initial_maintenance_inspection_report_2_mapper.selTable2(Case);
        Initial_maintenance_inspection_report_3 initialMaintenanceInspectionReport3 = initial_maintenance_inspection_report_3_mapper.selTable3(Case);
        //new一个DTO类
        Initial_maintenance_inspection_reportDTO initialMaintenanceInspectionReportDTO = new Initial_maintenance_inspection_reportDTO();
        //把子类的值拷贝到DTO类
        CopyUtil.copyClass(initialMaintenanceInspectionReport1,initialMaintenanceInspectionReportDTO );
        CopyUtil.copyClass(initialMaintenanceInspectionReport2,initialMaintenanceInspectionReportDTO);
        CopyUtil.copyClass(initialMaintenanceInspectionReport3,initialMaintenanceInspectionReportDTO);
        System.out.println(initialMaintenanceInspectionReportDTO);
        return initialMaintenanceInspectionReportDTO;
    }

    @Override
    public String updateTable1(Initial_maintenance_inspection_reportVO initialMaintenanceInspectionReportVO) {
        //时间表
        Initial_maintenance_inspection_report initialMaintenanceInspectionReport = new Initial_maintenance_inspection_report();
        //子表1
        Initial_maintenance_inspection_report_1 initialMaintenanceInspectionReport1 = new Initial_maintenance_inspection_report_1();
        //子表2
        Initial_maintenance_inspection_report_2 initialMaintenanceInspectionReport2 = new Initial_maintenance_inspection_report_2();
        //子表3
        Initial_maintenance_inspection_report_3 initialMaintenanceInspectionReport3 = new Initial_maintenance_inspection_report_3();
        //时间表 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport);
        //时间表 获取当前时间
        initialMaintenanceInspectionReport.setUpdatedAt(LocalDateTime.now());
        //子表1 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport1);
        //子表2 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport2);
        //子表3 获取VO类对应的值
        CopyUtil.copyClass(initialMaintenanceInspectionReportVO, initialMaintenanceInspectionReport3);
        int i = initial_maintenance_inspection_report_mapper.updateTableTime(initialMaintenanceInspectionReport);
        int i1 = initial_maintenance_inspection_report_1_mapper.updateTable1(initialMaintenanceInspectionReport1);
        int i2 = initial_maintenance_inspection_report_2_mapper.updateTable2(initialMaintenanceInspectionReport2);
        int i3 = initial_maintenance_inspection_report_3_mapper.updateTable3(initialMaintenanceInspectionReport3);
        AssertUtil.isTrue(i == 1 && i1 == 1 && i2 == 1 && i3 == 1,"表1修改数据出现异常,请检查");
        return "修改成功" ;
    }

    /**
     * 查询分页数据
     * @param page
     * @return
     */
    @Override
    public MainTableDTO queryPaginatedData(Integer page) {
        Integer number = (page - 1) * 10;
        return null;
    }

    /**
     * 查询总页数
     * @return
     */
    @Override
    public Integer queryPaginatedPage() {
        return 0;
    }
}
